Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add the validate utils #32

Merged
merged 12 commits into from
Jul 20, 2023
Merged

feat: add the validate utils #32

merged 12 commits into from
Jul 20, 2023

Conversation

duguorong009
Copy link
Contributor

@duguorong009 duguorong009 commented Jul 3, 2023

What kind of change does this PR introduce?

Add the validate utils for getting the actual validation errors thrown while validating the json schema and instance.

What is the current behavior?

Closes #14

What is the new behavior?

Receives the actual errors thrown, as Vec<String>

Additional context

@duguorong009 duguorong009 mentioned this pull request Jul 3, 2023
@olirice
Copy link
Collaborator

olirice commented Jul 5, 2023

hi @duguorong009 please take a look at the CI errors when you get a minute
thanks!

@duguorong009 duguorong009 marked this pull request as draft July 10, 2023 07:16
@duguorong009 duguorong009 marked this pull request as ready for review July 10, 2023 07:28
@olirice olirice self-requested a review July 10, 2023 20:14
@olirice
Copy link
Collaborator

olirice commented Jul 11, 2023

sorry for the delay getting back to you, its been a hectic week
will leave a review nlt tomorrow

src/lib.rs Outdated
@@ -29,6 +29,64 @@ fn jsonschema_is_valid(schema: Json) -> bool {
}
}

#[pg_extern(immutable, strict)]
fn validate_json_schema(schema: Json, instance: Json) -> bool {
Copy link
Collaborator

@olirice olirice Jul 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the linked issue I thought the goal was to produce functions that return an array of strings that described and errors that are present.

These two functions have identical signatures and behavior to json_matches_schema and jsonb_matches_schema but have raise notices that list the errors.

If that is the goal, why not update the existing two functions? e.g. is it a performance problem? if so, how big is the penalty?

If it is something like performance, an optional argument to the existing functions like on_error_raise that takes one of nothing (default), notice, or exception would be preferred to adding new functions

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, there is performance problem.
From the doc, the jsonschema::is_valid is much faster than validate function.

Anyhow, I removed the new functions, and updated the existing functions as recommended.
Please re-check it. @olirice

@olirice
Copy link
Collaborator

olirice commented Jul 18, 2023

@duguorong009 sorry for the back and forth

It's not clear to me what we're solving here. #14 requested exposing validate() which I understood to mean adding a function that would return an array of error strings.

Given that there is a performance hit for using validate vs is_valid we're trading performance for hints.

But check constraints won't bubble up those hints, logs would need to be set to verbose to include hints, and supabase studio doesn't display hints (AO 2023-07). So the only time the hints would be visible is if a user interactively connected to the database. Due to that, I'm not sure this is an appropriate trade to make.


If you'd be interested in changing this to a single immutable function (json only, not jsonb)

fn jsonschema_validation_errors(schema: Json, instance: Json) -> Vec<String>

that returns an array of errors or an empty array (if there are no errors) I'd be happy to approve

thanks!

@duguorong009
Copy link
Contributor Author

@duguorong009 sorry for the back and forth

It's not clear to me what we're solving here. #14 requested exposing validate() which I understood to mean adding a function that would return an array of error strings.

Given that there is a performance hit for using validate vs is_valid we're trading performance for hints.

But check constraints won't bubble up those hints, logs would need to be set to verbose to include hints, and supabase studio doesn't display hints (AO 2023-07). So the only time the hints would be visible is if a user interactively connected to the database. Due to that, I'm not sure this is an appropriate trade to make.

If you'd be interested in changing this to a single immutable function (json only, not jsonb)

fn jsonschema_validation_errors(schema: Json, instance: Json) -> Vec<String>

that returns an array of errors or an empty array (if there are no errors) I'd be happy to approve

thanks!

Thanks for the detailed tips! @olirice

@olirice
Copy link
Collaborator

olirice commented Jul 19, 2023

looks good

please include tests for 0 errors, 1 error, and multiple errors:

e.g.

// No errors
crate::jsonschema_validation_errors(
    Json(json!({ "maxLength": 4 })),
    Json(json!("foo")),
)

// One error
crate::jsonschema_validation_errors(
    Json(json!({ "maxLength": 4 })),
    Json(json!("123456789")),
)

// Multiple errors
crate::jsonschema_validation_errors(
    Json(json!(
    {
        "type": "object",
        "properties": {
            "foo": {
                "type": "string"
            },
            "bar": {
                "type": "number"
            },
            "baz": {
                "type": "boolean"
            },
            "additionalProperties": false,
            "required": ["foo", "bar", "baz"]
        }
    })),
    Json(json!({"foo": 1, "bar": [], "bat": true})),
)

src/lib.rs Outdated
})),
Json(json!({"foo": 1, "bar": [], "bat": true})),
);
assert!(errors.len() == 1);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there should be multiple errors on this one

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for comment!
Will update! @olirice

@olirice olirice merged commit 0d286ed into supabase:master Jul 20, 2023
1 check passed
@olirice
Copy link
Collaborator

olirice commented Jul 20, 2023

thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Expose validate()
2 participants